package mac5789.projeto4.algorithm;

import java.util.Collections;

import mac5789.projeto4.model.Policy;
import mac5789.projeto4.model.State;
import mac5789.projeto4.model.WorldModel;

/**
 * Implementação de Markov Decision Process que utiliza o algoritmo RTDP.
 * 
 * @author lundberg
 * 
 */
public class RTDPMDP implements MarkovDecisionProcess {

	private final double acceptableError;

	private int iteractions = 0;

	/**
	 * Construtor
	 * 
	 * @param acceptableError acceptableError
	 */
	public RTDPMDP(double acceptableError) {
		this.acceptableError = acceptableError;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Policy createPolicy(WorldModel world) {
		BellmanBackup backup = BellmanBackup.allZeroes(world, Collections.<State> emptySet());
		while (iteractions < world.getMaxIteractions() && backup.getBellmanError() > acceptableError) {
			iteractions++;
			RTDPMDPTrial trial = new RTDPMDPTrial(world, backup);
			trial.execute();
			backup = trial.improve();
		}
		return backup.createReachablePolicy();
	}

	public int getIteractions() {
		return iteractions;
	}
}
